package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.SchoolMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.entity.SchoolVo;
import com.lemeng.pojo.School;
import com.lemeng.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    SchoolMapper schoolMapper;

    @Override
    public List<School> findAll() {
        return schoolMapper.selectAll();
    }

    @Override
    public PageResult<School> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<School> schoolPageInfo = new PageInfo<>(schoolMapper.selectAll());
//        Page<School> schools = (Page<School>)schoolMapper.selectAll();
        return new PageResult(schoolPageInfo.getTotal(),schoolPageInfo.getList());
    }

    @Override
    public List<School> findList(Map<String, Object> serchMap) {
        Example example = new Example(School.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("letter")!=null && StringUtil.isNotEmpty(serchMap.get("letter").toString())){
                criteria.andEqualTo("letter",serchMap.get("letter").toString());
            }
        }
        return schoolMapper.selectByExample(example);
    }

    @Override
    public List<SchoolVo> selectCountInfo() {
        return schoolMapper.selectCountInfo();
    }

    @Override
    public PageResult<SchoolVo> findPage(Integer page, Integer size, SchoolVo schoolVo) {
        PageHelper.startPage(page,size);
        PageInfo<SchoolVo> schoolPageInfo = new PageInfo<>(schoolMapper.findSchools(schoolVo));
        return new PageResult(schoolPageInfo.getTotal(),schoolPageInfo.getList());
    }

    @Override
    public School findById(Integer id) {
        return schoolMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(School school) {
        schoolMapper.insert(school);
    }

    @Override
    public void update(School school) {
        schoolMapper.updateByPrimaryKeySelective(school);
    }

    @Override
    public void delete(Integer id) {
        schoolMapper.deleteByPrimaryKey(id);
    }


}
